﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TGS04
    {

        public TGS04()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VGSMATK();
            DataContext = MV;
            InitializeComponent();

        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }

        public void Refresh()
        {
            if (vwMaster == null)
                return;
            if (vwMaster.View == null)
                return;
            while (true)
            {
                vwMaster.View.MoveCurrentToNext();
                if (vwMaster.View.IsCurrentAfterLast)
                    break;
            }
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);

        }

        public override void OnSD13Chg(DataRow sd)
        {
            if (sd == null)
                return;
            if (dwMaster == null)
                return;

            dwMaster["GSMRBEZ"] = sd["KEY"].ToString().Trim().PadRight(6);
            dwMaster["GSMRSPZ"] = Helper.Left(Helper.ConvertGB2312To8859P1(sd["DES"].ToString()).Trim().PadRight(25), 25);
            dwMaster["GSMRFARBE"] = Helper.Left(Helper.ConvertGB2312To8859P1(sd["SDES"].ToString()).Trim().PadRight(10), 10);
            string des = "";
            foreach (DataRow row in TBMain.Rows)
            {
                des += row["GSMRBEZ"].ToString() + ",";
            }
            des += "***";
            DES = des.Replace(",***", "");
            base.OnSD13Chg(sd);
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("请选择一行");
                return;
            }
            IsGSSave = false;
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            dwData["GSMPOS"] = Helper.Right("00" + GetPos(), 2);
            TBMain.Rows.InsertAt(dwData, 0);
            //TMV.DeleteGSMATK(dwData, 1);
            TMV.InsertGSMATK(dwData, app.company);
            vwMaster.View.MoveCurrentToFirst();
            base.NewCommand(sender, e);
        }
        public int GetPos()
        {
            int matpos = 0;
            foreach (DataRow row in TBMain.Rows)
            {
                int val = Convert.ToInt16(row["GSMPOS"].ToString());
                if (val > matpos)
                    matpos = val;
            }
            matpos++;
            return matpos;
        }
        public override void LOADBYAUF()
        {
            VAR = "  ";
            TBMain.Clear();
            IsGSSave = true;
            if (ORDERNUM.Length < 29)
                return;
            LFD = ORDERNUM;
            dwData = VVAKDBEZ.Instance.TBMain.NewRow();
            dwData["VAKBMANDANT"] = app.Mandant;
            dwData["VAKBKOLL"] = ORDERNUM.Substring(0, 5);
            dwData["VAKBART"] = ORDERNUM.Substring(5, 24);
            dwData["VAKBFARBE"] = ORDERNUM.Substring(29, 10);
            dwData["VAKBKUNDNR"] = ORDERNUM.Substring(39, 8);
            SResult = VVAKDBEZ.Instance.QueryVAKDBEZ(dwData, 1);
            foreach (DataRow row in SResult.Tables[0].Copy().Rows)
            {
                dwVAK = row;
            }
            dwData = TBMain.NewRow();
            dwData["GSMMANDANT"] = app.Mandant;
            dwData["GSMART"] = ORDERNUM.Substring(29, 10).PadRight(20);
            SResult = TMV.QueryGSMATK(dwData, 6);
            MergeData(SResult, false, false);
            if (!IsAuto)
            {
                return;
            }

            if (TBMain.Rows.Count > 0)
                return;
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["GSMMANDANT"] = app.Mandant;
            dwData["GSMART"] = ORDERNUM.Substring(29, 10).PadRight(20);
            dwData["GSMPOS"] = Helper.Right("00" + GetPos(), 2);
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.DGSM01(dwData);
            
            TMV.InsertGSMATK(dwData, app.company);
            vwMaster.View.MoveCurrentToFirst();
            IsEdit = true;
            IsGSSave = false;
            GetInfo();
            base.LOADBYAUF();
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (TBMain.Rows.Count < 2)
            {
                MessageBox.Show("不可以");
                return;
            }
            filter = "GSMPOS='" + dwMaster["GSMPOS"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }

        int matpos = 1;
        /// <summary>
        /// 1.成份流水的前提是系列+流水相同 
        ///2.从献捷导入的没有系列+流水 
        ///3.所以算成份流水时，不会考虑那部分数据
        /// </summary>
        private void SaveGSMATK()
        {

            string c = "";
            string yc = "";
            string a = "";
            matpos = 1;
            double pecentage = 0;
            int var = 0;  
             sort = "GSMRFARBE ASC";
            filter = "";
            rows = TBMain.Select(filter, sort);
            foreach (DataRow row in rows)
            {
                var = Convert.ToInt16(row["GSMVAR"].ToString());
                string sv = row["GSMRANT"].ToString();
                if (sv == "")
                    sv = "0";
                if (sv == "0") break;
                c = row["GSMRFARBE"].ToString().Trim();
                a = row["GSMRBEZ"].ToString().Trim();
                double ratio = 0.1 * Convert.ToDouble(sv);
                if (c != "C")
                {
                    if (c == "T")
                    {
                        if (ratio < 50)
                            c = "V";
                    }
                    yc += c;
                }
                else
                {
                    if (a != "CO")
                        yc += c;
                }
                pecentage += ratio;
                matpos++;
            }
            if (pecentage != 100)
                System.Windows.MessageBox.Show("比例不等于100，请检查");
            matpos = 1;
            if (dwVAK == null)
                return;
            PM["RUNID"] = this.RUNID;
            PM["KOLL"] = dwVAK["VAKBKOLL"].ToString();
            PM["ART"] = dwVAK["VAKBART"].ToString();
            PM["FARBE"] = dwVAK["VAKBFARBE"].ToString();
            PM["KUNDER"] = dwVAK["VAKBKUNDNR"].ToString();
            VVAKDBEZ.Instance.VAKDCHK(PM);
            string VAKBART = dwVAK["VAKBART"].ToString();
            string VAKBFARBE = dwVAK["VAKBFARBE"].ToString();
            int lg = VAKBART.Length;
            int first1 = VAKBART.IndexOfAny(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
            if (first1 > 0)
            {

                DataRow dwVF = VVAFARBEN.Instance.TBMain.NewRow();
                dwVF["VAFMANDANT"] = app.Mandant;
                dwVF["VAFKOLL"] = dwVAK["VAKBKOLL"];
                dwVF["VAFART"] = dwVAK["VAKBART"];
                dwVF["VAFFILLER"] = dwVAK["VAKBFARBE"];
                PM["RUNID"] = this.RUNID;
                PM["KOLL"] = dwVAK["VAKBKOLL"].ToString();
                PM["ART"] = dwVAK["VAKBART"].ToString();
                PM["FARBE"] = dwVAK["VAKBFARBE"].ToString();
                PM["KUNDER"] = dwVAK["VAKBKUNDNR"].ToString();
                if (!IsManual)
                {
                    if (var > 0)
                    {

                        VAKBFARBE = VAKBART.Substring(0, first1) + var + yc + VAKBART.Substring(first1);
                    }
                    else
                    {
                        VAKBFARBE = VAKBART.Substring(0, first1) + yc + VAKBART.Substring(first1);
                    }
                }
                else
                {
                    VAKBFARBE = FARBE.ToUpper().Trim().PadRight(10);
                }
                if (VAKBFARBE.Trim().Length > 10)
                {
                    FARBE = VAKBFARBE;
                    IsManual = true;
                    MessageBox.Show("色号长度超过10位，请修正色号");
                    return;
                }
                VAKBFARBE = VAKBFARBE.ToUpper();
                dwVAK["VAKBFILLER"] = dwVAK["VAKBFARBE"];
                dwVAK["VAKBFARBE"] = Helper.Left(VAKBFARBE, 10);

                dwVF["VAFFARBE"] = dwVAK["VAKBFARBE"];
                if (dwVAK["VAKBFILLER"].ToString() != dwVAK["VAKBFARBE"].ToString())
                {
                    SResult = VVAKDBEZ.Instance.QVAK10(dwVAK);
                    if (SResult.Tables[0].Rows.Count > 0)
                    {
                        IsManual = true;
                        FARBE = VAKBFARBE;
                        MessageBox.Show(dwVAK["VAKBFARBE"].ToString() + "色号已经存在");
                        return;
                    }
                }

                dwData = TBMain.Rows[0];
                dwData["GSMART"] = Helper.Left(VAKBFARBE, 10).PadRight(20);
                TMV.DGSM02(dwData);
                foreach (DataRow row in TBMain.Rows)
                {
                    row["GSMART"] = Helper.Left(VAKBFARBE, 10).PadRight(20);
                    row["GSMFILLER"] = app.User;
                    TMV.InsertGSMATK(row, app.company);

                }
               /* DataRow dwSD = TMV.VSD.TBMain.NewRow();
                dwSD["SDSA"] = "0014";
                dwSD["SDDATEN"] = "00" + Helper.Left(VAKBFARBE.PadRight(21), 21) + "V"; ;//新色号
                dwSD["SDKEY"] = "00" + Helper.Left(dwVAK["VAKBFILLER"].ToString().PadRight(21), 21) + "V"; ;//现有的键值 
                TMV.VSD.USD03(dwSD);
                dwSD["SDDATEN"] = "00" + Helper.Left(VAKBFARBE.PadRight(21), 21) + "E"; ;//新色号
                dwSD["SDKEY"] = "00" + Helper.Left(dwVAK["VAKBFILLER"].ToString().PadRight(21), 21) + "E"; ;//现有的键值 
                TMV.VSD.USD03(dwSD);
                dwSD["SDDATEN"] = "00" + Helper.Left(VAKBFARBE.PadRight(21), 21) + " "; ;//新色号
                dwSD["SDKEY"] = "00" + Helper.Left(dwVAK["VAKBFILLER"].ToString().PadRight(21), 21) + " "; ;//现有的键值 
                TMV.VSD.USD03(dwSD);*/
                DataRow dwTX = TMV.VTXT.TBMain.NewRow();
                dwTX["TXTIFILLER"] = Helper.Left((dwVAK["VAKBKOLL"].ToString() + dwVAK["VAKBART"].ToString() + dwVAK["VAKBKUNDNR"].ToString().Trim().PadRight(8) + VAKBFARBE).PadRight(60), 60);//新色号
                dwTX["TXTISIND"] = Helper.Left((dwVAK["VAKBKOLL"].ToString() + dwVAK["VAKBART"].ToString() + dwVAK["VAKBKUNDNR"].ToString().Trim().PadRight(8) + dwVAK["VAKBFILLER"].ToString()).PadRight(60), 60); //现有的键值                

                TMV.VTXT.UTXT02(dwTX);

                VVAKDBEZ.Instance.UpdateVAKDBEZ(dwVAK, app.company, 2);
               // VVAFARBEN.Instance.DeleteVAFARBEN(dwVF, 1);
                VVAFARBEN.Instance.UpdateVAFARBEN(dwVF, app.company, 5);

                PM["NFARBE"] = dwVAK["VAKBFARBE"].ToString();
                VVAKDBEZ.Instance.VAKDFARBECHG(PM);
                //MessageBox.Show("色号被成分重置，请重新查询后，再编辑其他信息");
            }
            FARBE = "";
            IsManual = false;
            cSSD0013.KEY = "";
        }
         
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (TBMain.Rows.Count == 0)
                    return;
               TMV.DGSM02(TBMain.Rows[0]);     
                SaveGSMATK();
                IsGSSave = true; 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            base.SaveCommand(sender, e);
        }



        public override void OnVARChg(string var)
        {
            foreach (DataRow row in TBMain.Rows)
            {
                row["GSMVAR"] = var;
            }
            base.OnVARChg(var);
        }
    }
}
